﻿/**
 * ...
 * @author aboutwhat
 */

package format.hxml;

import haxe.io.BytesOutput;
import haxe.io.StringInput;
import format.hxml.Data.Data;
import format.hxml.Data.Option;
import format.hxml.Data.Library;

enum Target 
{
    neko;
    cpp;
    js;
    swf(?version : Int);
    php;
    as3;
    none;
    invalid;
}

class Tools 
{
    
    public static function getLibs (h : Data) : List<Library>
    {
        var l = new List();
        for (o in h) {
            switch(o) {
                case lib(v) : l.add(v);
                default : 
            }
        }
        return l;
    }
    
    public static function getMain (h : Data) : String
    {
        
        for (o in h) {
            switch(o) {
                case main(v) : return v;
                default : 
            }
        }
        return null;
    }
    
    public static function stringToHxml (str:String) 
    {
        var reader = new Reader(new StringInput(str));
        
        return reader.read();
    }
    
    public static function hxmlToString (h : Data) {
        var output = new BytesOutput();
        var writer = new Writer(output);
        writer.write(h);
        return output.getBytes().toString();
    }
    
    public static function getTarget (h : Data) : Target
    {
        var tar = Target.none;
        
        if (isSingleHxml(h)) 
        {
            var swfversion : Int = -1;
            for (o in h) 
            {    
                switch (o) {
                    case swfVersion(v) : swfversion = v;
                    default : null;
                };
                var newTarget = switch (o) {
                    case neko(_) :       Target.neko;
                    case swf(_) : Target.swf(8);
                    case swf9(_) : Target.swf(9);
                    case cpp(_) :  Target.cpp;
                    case php(_) :  Target.php;
                    case js(_) :   Target.js;
                    case as3(_) :   Target.as3;
                    default : none;
                }
                
                if (newTarget != Target.none) {
                    if (tar != Target.none) {
                        return Target.invalid;
                    } else 
                        tar = newTarget;
                }
                
            }
            if (swfversion != -1) {
                tar = switch (tar) {
                    case swf(_) : Target.swf(swfversion);
                    default : tar;
                }
            }
        }
        
        return tar;
    }
    
    public static function countCompilations (h : Data)
    {
        var numComp = 1;
        for (o in h) {
            switch (o) {
                case next : numComp++;
                default : 
            }
        }
        return numComp;
    }
    
    public static function splitCompilations (h : Data) : List<Data>
    {
        var res = new List<Data>();
        var cur = new Data();
        res.add(cur);
        for (o in h) {
            switch(o) {
                case next :
                    cur = new List();
                    res.add(cur);
                default : cur.add(o);
            }
        }
        return res;
    }
    
    public static function hasMultipleCompilations (h : Data) : Bool 
    {
        for (o in h) {
            switch (o) {
                case next : return true;
                default : 
            }
        }
        return false;
    }
    
    public static function isSingleCompilation (h : Data) : Bool 
    {
        return !hasMultipleCompilations(h);
    }
    
    public static function combineCompilations (l : List<Data>) : Data {
        var res = new List();
        
        var first = true;
        for (sub in l) {
            if (!first) res.push(Option.next) else first = false;
            for (o in sub) {
                res.push(o);
            }
        }
        return res;
    }
}